<template>
    <div>
       
        <ul class="list">
            <li class="item"   v-for="item in letters" :key = "item" :ref = "item"  @click="handleLetterClick" @touchstart = "handleTouchStart" @touchmove="handleTouchMove" @touchend = "handleTouchEnd" >
            {{ item }}
            </li>
        </ul>
    </div>
</template>

<script>
export default {
  props: {
    cities: Object
  },
  data() {
    return {
      timer: null,
      startY: 0,
      touchStatus: false
    };
  },
  updated() {
    this.startY = this.$refs["A"][0].offsetTop;
  },
  methods: {
    handleLetterClick(e) {
      console.log('111',e.target.innerText);
      this.$emit("change", e.target.innerText); //发送
    },
    handleTouchStart() {
      this.touchStatus = true;
    },
    handleTouchMove(e) {
      console.log('123');
      this.timer = setTimeout(() => {
        const touchY = e.touches[0].clientY - 79;
        console.log('touchY',touchY);
        const index = Math.floor((touchY - this.startY) / 20);
        console.log(index);
        if (index >= 0 && index < this.letters.length) {
          this.$emit("change", this.letters[index]);
        }
      }, 16);
    },
    handleTouchEnd() {
      this.touchStatus = false;
    }
  },
  computed: {
    letters() {
      const letters = [];
      for (let i in this.cities) {
        letters.push(i);
      }
      return letters;
    }
  }
};
</script>

<style lang="scss" scoped>
.list {
  display: flex;
  flex-direction: column;
  justify-content: center;
  position: absolute;
  top: 1.58rem;
  right: 0;
  bottom: 0;
  width: 0.44rem;
  z-index: 1;
  .item {
    line-height: 0.4rem;
    text-align: 0.4rem;
    color: #00bcd4;
  }
}
</style>